class Solution {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        int index = 0;
        Stack<Integer> s = new Stack<>(); 
        for(int i : pushed){
            s.add(i);
             while(!s.isEmpty() && s.peek() == popped[index]){
                s.pop();
                index++;
            }
        }
        return s.isEmpty();
    }
}